<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        #app{
            border: 2px solid black;
            padding: 10px;
            margin-bottom: 400px;
        }
        .child{
            margin: 20px;
            padding: 10px;
            background-color: #cccccc;
        }
    </style>
</head>
<body>
    <div id="app">
        <h3>父组件范围</h3>
        <p>info:{{info}}</p>
        <input type="button" value="获取子组件数据" @click="getInfo()">
        <!-- <child-one></child-one> -->
        <child-one v-on:send="setInfo"></child-one>
    </div>

    <template id="ChildOne">
        <div class="child">
            <h3>子组件</h3>
            <p>info:{{info}}</p>
            <input type="text" v-model="info">
            <input type="button" value="发送数据" @click="sendInfo()">
            <input type="button" value="发送数据$emit" @click="sendInfo2()">
        </div>
    </template>
</body>
<script>
    Vue.component("ChildOne",{
        template:"#ChildOne",
        data(){
            return {
                info:"子组件变量info"
            }
        },
        methods: {
            sendInfo(){
                // 2.在子组件中调用$parent获取父组件的实例，不具有响应式功能
                console.log(this.$parent);
                this.$parent.info=this.info;
            },
            sendInfo2(){
                console.log(this);
                
                this.$emit("send",this.info);
            }
        },

    });

    new Vue({
        el:"#app",
        data:{
            info:""
        },
        methods: {
            getInfo(){
            // 1.$children获取子组件实例
            //       + 不具有响应式功能
            //       + 获取的子组件对象数组，顺序并不一定按照页面组件调用顺序存放
                console.log(this.$children);
                this.info = this.$children[0].info;
            },
            setInfo(arg){
                this.info=arg;
            }
        },
    });
</script>
</html>